package com.kelvin.paopao.module.person.detail;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.kelvin.kelvinlib.imageloader.GlideUtil;
import com.kelvin.kelvinlib.utils.CommonUtil;
import com.kelvin.paopao.R;
import com.kelvin.paopao.adapter.PersonPhotoAdapter;
import com.kelvin.paopao.api.responseBean.PersonPhotoModel;
import com.kelvin.paopao.api.responseBean.UserDetailModel;
import com.kelvin.paopao.injector.components.DaggerPersonDetailComponent;
import com.kelvin.paopao.injector.modules.PersonDetailModule;
import com.kelvin.paopao.module.base.BaseActivity;
import com.kelvin.paopao.module.chat.chat.ChatActivity;
import com.kelvin.paopao.module.person.photoset.PhotoSetActivity;
import com.kelvin.paopao.utils.Constant;
import com.kelvin.paopao.utils.LoginUtil;
import com.kelvin.paopao.widgets.DrawableTextView;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;
import jp.wasabeef.glide.transformations.BlurTransformation;

/**
 * Created by Administrator on 2017/5/5.
 */
public class PersonDetailActivity extends BaseActivity<IPersonDetailPresenter>
        implements IPersonDetailView {

    @BindView(R.id.iv)
    CircleImageView iv;
    @BindView(R.id.iv_gender)
    ImageView ivGender;
    @BindView(R.id.tv_age)
    TextView tvAge;
    @BindView(R.id.tv_location)
    TextView tvLocation;
    @BindView(R.id.tv_distance)
    TextView tvDistance;
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.tv_tall)
    TextView tvTall;
    @BindView(R.id.tv_weight)
    TextView tvWeight;
    @BindView(R.id.tv_job)
    TextView tvJob;
    @BindView(R.id.activity_main)
    RelativeLayout activityMain;
    @BindView(R.id.tv_relationship)
    TextView tvRelationship;
    @BindView(R.id.tv_qianming)
    TextView tvQianming;
    @BindView(R.id.tv_focus)
    DrawableTextView tvFocus;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.fl_focus)
    FrameLayout flFocus;
    @BindView(R.id.tv_chat)
    DrawableTextView tvChat;
    @BindView(R.id.fl_chat)
    FrameLayout flChat;
    @BindView(R.id.appbar)
    AppBarLayout appbar;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.id_flowlayout)
    TagFlowLayout idFlowlayout;
    @BindView(R.id.iv_bg)
    ImageView ivBg;
    @BindView(R.id.ll)
    LinearLayout ll;

    private String user_no;

    @Inject
    PersonPhotoAdapter adapter;

    private LinearLayoutManager manager;
    private String user_label;
    private TagAdapter tagAdapter;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_person_detail;
    }

    @Override
    protected void initInjector() {
        DaggerPersonDetailComponent.builder().applicationComponent(getAppComponent()).
                personDetailModule(new PersonDetailModule(this)).build().inject(this);
    }

    @Override
    protected void initViews() {

        user_no = getIntent().getStringExtra("user_no");
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyclerView.setLayoutManager(manager);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                PhotoSetActivity.launch(PersonDetailActivity.this, imgs, position);
            }
        });
        recyclerView.setAdapter(adapter);

        appbar.addOnOffsetChangedListener(
                new AppBarLayout.OnOffsetChangedListener() {
                    @Override
                    public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
                        if (Math.abs(verticalOffset) > 180) {
                            tvTitle.setVisibility(View.VISIBLE);
                        } else {
                            tvTitle.setVisibility(View.GONE);
                        }
                    }
                });

    }

    @Override
    protected void updateViews(boolean isRefresh) {
        mPresenter.getUsetDetial(user_no);
        mPresenter.checkFocus(user_no);
        mPresenter.getUserPhoto(user_no);
    }


    @OnClick({R.id.fl_focus, R.id.fl_chat})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.fl_focus:
                if (Constant.IS_LOGIN) {
                    mPresenter.changefocus(user_no);
                } else {
                    LoginUtil.checkLogin(this);
                }
                break;
            case R.id.fl_chat:
                if (Constant.IS_LOGIN) {
                    ChatActivity.launch(this, user_no);
                } else {
                    LoginUtil.checkLogin(this);
                }
                break;
        }
    }

    public static void launch(Context context, String user_no) {
        Intent intent = new Intent(context, PersonDetailActivity.class);
        intent.putExtra("user_no", user_no);
        context.startActivity(intent);
    }

    @Override
    public void loadData(UserDetailModel model) {
        user_label = model.getUser_Label();
        if (!TextUtils.isEmpty(user_label)) {
            tagAdapter = new TagAdapter<String>(user_label.split("\\|")) {
                @Override
                public View getView(FlowLayout parent, int position, String s) {
                    TextView tv = (TextView) LayoutInflater.from(PersonDetailActivity.this)
                            .inflate(R.layout.tag_item,
                                    idFlowlayout, false);
                    tv.setText(s);
                    return tv;
                }
            };
            idFlowlayout.setAdapter(tagAdapter);
        }

        GlideUtil.loadImgCircle(model.getUser_headimg(),
                iv, PersonDetailActivity.this, R.drawable.default_img);

        //加载告诉模糊的背景图

        Glide.with(this)
                .load(model.getUser_headimg()).centerCrop()
                .bitmapTransform(new BlurTransformation(this, 25, 4))  // “23”：设置模糊度(在0.0到25.0之间)，默认”25";"4":图片缩放比例,默认“1”。
                .into(ivBg);

        tvTitle.setText(model.getUser_name());
        tvName.setText(model.getUser_name());
        if (model.getUser_sex() == 1) {
            ivGender.setImageResource(R.drawable.icon_male_color);
        } else {
            ivGender.setImageResource(R.drawable.icon_female_color);
        }

        if (!TextUtils.isEmpty(model.getUser_age())) {
            tvAge.setText(CommonUtil.getAge(model.getUser_age()) + "岁");
        }

        if (!TextUtils.isEmpty(model.getUser_city()) ||
                !TextUtils.isEmpty(model.getUser_province())) {
            tvLocation.setText(model.getUser_province() + " " + model.getUser_city());
        }

        if (!TextUtils.isEmpty(model.getUser_qianming())) {
            tvQianming.setText(model.getUser_qianming());
        }

        if (!TextUtils.isEmpty(model.getUser_shengao())) {
            tvTall.setText(model.getUser_shengao());
        }

        if (!TextUtils.isEmpty(model.getUser_tizhong())) {
            tvWeight.setText(model.getUser_tizhong());
        }

        if (!TextUtils.isEmpty(model.getUser_ganqing())) {
            tvRelationship.setText(model.getUser_ganqing());
        }

        if (!TextUtils.isEmpty(model.getUser_zhiye())) {
            tvJob.setText(model.getUser_zhiye());
        }
    }

    @Override
    public void setFocus() {
        tvFocus.setText("取消关注");
    }

    @Override
    public void setUnfocus() {
        tvFocus.setText("关注");
    }

    private List<String> imgs;

    @Override
    public void loadPhoto(List<PersonPhotoModel> photos) {
        if (photos != null && photos.size() != 0) {
            imgs = new ArrayList<>();
            for (PersonPhotoModel personPhotoModel : photos) {
                imgs.add(personPhotoModel.getUserimg());
            }
            adapter.setNewData(imgs);
        }
    }

}
